// pages/order/order.js
let app = getApp()
Page({

  data: {
    orderList: [],
    orderListLength: '',
    page: 1,
    store_id: ''
  },

  // 获取数据
  getOrderList(that, params) {
    wx.request({
      url: app.globalData.host + 'distribution/cash_list',
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      data: params,
      success(res) {
        if (res.data.code == 2000) {
          let list = that.data.orderList
          let list1 = res.data.data;
          if (params.page == 1) {
            list = list1
          } else {
            list = [...list, ...list1]
          }
          list.forEach(item =>{
            item.receipt_time= that.getDate(new Date(item.receipt_time*1000))
          })
          that.setData({
            orderList: list,
            orderListLength: list1.length
          })
        }
      },
      fail(err) {
        wx.showToast({
          title: '服务器开小差了~',
        })
      }
    })
  },

  onLoad: function (options) {
    const that = this;
    let store_id = options.store_id;
    that.setData({
      store_id: store_id
    })
    let params = {
      token: wx.getStorageSync('token'),
      store_id: store_id,
      page: that.data.page
    };
    that.getOrderList(that, params)
  },
  // 上拉加载
  onReachBottom: function () {
    let page = this.data.page || 1;
    page++;
    if (this.data.orderListLength < 10) {
      wx.showToast({
        title: '暂无更多',
        icon: 'none'
      })
      return
    }
    let that = this
    let params = {
      store_id: that.data.store_id,
      token: wx.getStorageSync('token'),
      page: page
    }
    that.getOrderList(that, params)
  },
  // 时间戳转换日期
  getDate(now) {
    var year = now.getFullYear(); //取得4位数的年份
    var month = now.getMonth() + 1; //取得日期中的月份，其中0表示1月，11表示12月
    var date = now.getDate(); //返回日期月份中的天数（1到31）
    var hour = now.getHours(); //返回日期中的小时数（0到23）
    var minute = now.getMinutes(); //返回日期中的分钟数（0到59）
    var second = now.getSeconds(); //返回日期中的秒数（0到59）
    return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
  },
})